package org.jeecg.modules.tls.oes.question.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.tls.oes.question.entity.OesQuestion;
import org.jeecg.modules.tls.oes.question.service.IOesQuestionService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.tls.oes.viewmodel.IntegerVM;
import org.jeecg.modules.tls.oes.viewmodel.admin.question.QuestionEditRequestVM;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: oes_question
 * @Author: jeecg-boot
 * @Date:   2022-07-15
 * @Version: V1.0
 */
@Api(tags="oes_question")
@RestController
@RequestMapping("/question/oesQuestion")
@Slf4j
public class OesQuestionController extends JeecgController<OesQuestion, IOesQuestionService> {
	@Autowired
	private IOesQuestionService oesQuestionService;
	
	/**
	 * 分页列表查询
	 *

	 */
	//@AutoLog(value = "oes_question-分页列表查询")

//	@GetMapping(value = {"/list/{pageIndex}/{pageSize}/{questionTypeStr}/{subjectIdStr}/{levelStr}","/list/{pageIndex}/{pageSize}"})
//	public Result getQuestionPage(@PathVariable Integer pageIndex,@PathVariable Integer pageSize,@PathVariable(required = false) String questionTypeStr,@PathVariable(required = false) String subjectIdStr,
//							 @PathVariable(required = false) String levelStr){
//		Integer questionType = null;
//		Integer subjectId = null;
//		Integer level = null;
//		if(!(questionTypeStr==null)&&(!questionTypeStr.equals("null"))){
//			questionType = Integer.valueOf(questionTypeStr);
//		}
//		if(!(subjectIdStr==null)&&(!subjectIdStr.equals("null"))){
//			subjectId = Integer.valueOf(subjectIdStr);
//		}
//		if(!(levelStr==null)&&(!levelStr.equals("null"))){
//			level = Integer.valueOf(levelStr);
//		}
//		return Result.ok(oesQuestionService.getQuestionPge(questionType,subjectId,level,pageIndex,pageSize));
//	}
	@ApiOperation(value="oes_question-分页列表查询", notes="oes_question-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<OesQuestion>> queryPageList(OesQuestion oesQuestion,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<OesQuestion> questionQueryWrapper = QueryGenerator.initQueryWrapper(oesQuestion, req.getParameterMap());;
		IPage<OesQuestion> pageList = oesQuestionService.getQuestionPge(questionQueryWrapper,pageNo,pageSize);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @return
	 */
//	@AutoLog(value = "oes_question-添加")
//	@ApiOperation(value="oes_question-添加", notes="oes_question-添加")
//	@PostMapping(value = "/add")
//	public Result<String> add(@RequestBody OesQuestion oesQuestion) {
//		oesQuestionService.save(oesQuestion);
//		return Result.OK("添加成功！");
//	}

	@PostMapping("/add")
	public Result addQuestion(@RequestBody QuestionEditRequestVM model){
		if(model.getId()==null||model.getId().equals("null")){
			oesQuestionService.insertQuestion(model);
		}else {
			oesQuestionService.updateQuetion(model);
		}

		return Result.ok(true);
	}

	/**
	 *  编辑
	 *
	 * @param oesQuestion
	 * @return
	 */
	@AutoLog(value = "oes_question-编辑")
	@ApiOperation(value="oes_question-编辑", notes="oes_question-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody OesQuestion oesQuestion) {
		oesQuestionService.updateById(oesQuestion);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
//	@AutoLog(value = "oes_question-通过id删除")
//	@ApiOperation(value="oes_question-通过id删除", notes="oes_question-通过id删除")
//	@DeleteMapping(value = "/delete")
//	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
//		oesQuestionService.removeById(id);
//		return Result.OK("删除成功!");
//	}

	@DeleteMapping("/delete")
	public Result deleteQuetion(@RequestParam(name="id",required=true) Integer id){
		oesQuestionService.deleteQuestion(id);
		return Result.ok(true);

	}

	 @GetMapping("{id}")
	 public Result getQuetionById(@PathVariable Integer id){
		 return Result.ok(oesQuestionService.getQuestionById(id));
	 }

	 @PostMapping("getquestions")
	 public Result getQuestionsById(@RequestBody IntegerVM integerVM){
		List<QuestionEditRequestVM> questionEditRequestVMList = new ArrayList<>();
		for (Integer id : integerVM.getIds()){
			QuestionEditRequestVM questionEditRequestVM = oesQuestionService.getQuestionById(id);
			questionEditRequestVMList.add(questionEditRequestVM);
		}
		return Result.ok(questionEditRequestVMList);

	 }


	 @PutMapping
	 public Result updateQuestion(@RequestBody QuestionEditRequestVM model){
		 oesQuestionService.updateQuetion(model);
		 return Result.ok(true);
	 }
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "oes_question-批量删除")
	@ApiOperation(value="oes_question-批量删除", notes="oes_question-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.oesQuestionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "oes_question-通过id查询")
	@ApiOperation(value="oes_question-通过id查询", notes="oes_question-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<OesQuestion> queryById(@RequestParam(name="id",required=true) String id) {
		OesQuestion oesQuestion = oesQuestionService.getById(id);
		if(oesQuestion==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(oesQuestion);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param oesQuestion
    */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, OesQuestion oesQuestion) {
//        return super.exportXls(request, oesQuestion, OesQuestion.class, "oes_question");
//    }
    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OesQuestion.class);
    }
}
